//
//  HYUserCenterView.m
//  HaiYue
//
//  Created by skyline on 2018/1/5.
//  Copyright © 2018年 skyline. All rights reserved.
//

#import "HYUserCenterView.h"
#import "HYWalletBtn.h"
#import "HYUserFuncButton.h"
@interface HYUserCenterView ()
@property (nonatomic, strong) HYWalletBtn *wallteBtn;
@property (nonatomic, strong) HYWalletBtn *cashBtn;
@property (nonatomic, strong) UIView *line;
@property (nonatomic, strong) HYLabel *shoutuLBL;//收徒链接
@property (nonatomic, strong) HYLabel *copLBL;//复制
@property (nonatomic, strong) HYLabel *shareLBL;//分享
@property (nonatomic, strong) HYButton *redBtn;//红包规则
@property (nonatomic, strong) HYLabel *linkLBL;//链接
@property (nonatomic, strong) UIView *midLine;//
@property (nonatomic, strong) HYLabel *vipLBL;//vip等级
@property (nonatomic, strong) HYLabel *myMasterLBL;//我的师傅
@property (nonatomic, strong) HYLabel *zttgLBL;//昨天通过徒弟获得积分
@property (nonatomic, strong) HYButton *yijianbaishiBtn;//一键拜师
@property (nonatomic, strong) HYButton *tudishenqingBtn;//徒弟申请
@property (nonatomic, strong) HYLabel *tipsLBL;//
@end
@implementation HYUserCenterView
- (instancetype)init
{
    self = [super init];
    if (self)
    {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = CornerRadius;
        self.line.hidden = NO;
        self.cashBtn.hidden = self.wallteBtn.hidden = NO;
        [self setupSubView];
        if (IS_APPStore)
        {
            self.copLBL.hidden = NO;
            self.yijianbaishiBtn.hidden = NO;
            self.tudishenqingBtn.hidden = NO;
            self.tipsLBL.hidden = NO;
        }
        
    }
    return self;
}

- (HYWalletBtn *)wallteBtn
{
    if (!_wallteBtn)
    {
        _wallteBtn = [[HYWalletBtn alloc] initWithTitle:@"钱包" imageName:@"helpcenter_money_mark"];
        _wallteBtn.tag = HYUserCenterViewEventWallte;
        [_wallteBtn addTarget:self action:@selector(actionForClickedButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_wallteBtn];
        [_wallteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.top.equalTo(self);
            make.right.equalTo(self.mas_centerX);
            make.height.mas_equalTo(ButtonHeight);
        }];
    }
    return _wallteBtn;
}

- (HYWalletBtn *)cashBtn
{
    if (!_cashBtn)
    {
        _cashBtn = [[HYWalletBtn alloc] initWithTitle:@"代金劵" imageName:@"helpcenter_moneyticket_iv"];
        _cashBtn.tag = HYUserCenterViewEventIntegral;
        [_cashBtn addTarget:self action:@selector(actionForClickedButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_cashBtn];
        [_cashBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.mas_centerX);
            make.top.equalTo(self);
            make.right.equalTo(self);
            make.height.mas_equalTo(ButtonHeight);
        }];
    }
    return _cashBtn;
}

- (UIView *)line
{
    if (!_line)
    {
        _line = [[UIView alloc]init];
        _line.backgroundColor = LINE_COLOR;
        [self addSubview:_line];
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.right.equalTo(self);
            make.top.equalTo(self.wallteBtn.mas_bottom);
            make.height.mas_equalTo(CellBottomLineHeight);
        }];
    }
    return _line;
}

- (HYLabel *)shoutuLBL
{
    if (!_shoutuLBL)
    {
        _shoutuLBL = [[HYLabel alloc] initWithText:@"收徒链接" textColor:TEXT_BLACK_COLOR textFont:[UIFont systemFontOfSize:ContentFont weight:UIFontWeightLight]];
        [self addSubview:_shoutuLBL];
        [_shoutuLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(GAP);
            make.top.equalTo(self.line.mas_bottom).with.offset(GAP);
        }];
    }
    return _shoutuLBL;
}

- (HYButton *)redBtn
{
    if (!_redBtn)
    {
        _redBtn = [[HYButton alloc]initWithTitle:@"红包规则" backColor: ORANGE_COLOR];
        [_redBtn addTarget:self action:@selector(actionForClickedButton:) forControlEvents:UIControlEventTouchUpInside];
        _redBtn.titleLabel.font = [UIFont systemFontOfSize:TipsFont weight:UIFontWeightLight];
        _redBtn.tag = HYUserCenterViewEventRedRule;
        [self addSubview:_redBtn];
        [_redBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).with.offset(-GAP);
            make.centerY.equalTo(self.shareLBL);
        }];
    }
    return _redBtn;
}

- (HYLabel *)shareLBL
{
    if (!_shareLBL)
    {
        _shareLBL = [[HYLabel alloc] initWithText:@"分享" textColor:RED_COLOR textFont:[UIFont systemFontOfSize:ContentFont weight:UIFontWeightLight]];
        [self addSubview:_shareLBL];
        [_shareLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self.redBtn.mas_left).with.offset(-GAP);
            make.top.equalTo(self.line.mas_bottom).with.offset(GAP);
        }];
    }
    return _shareLBL;
}

- (HYLabel *)copLBL
{
    if (!_copLBL)
    {
        _copLBL = [[HYLabel alloc] initWithText:@"复制" textColor:RED_COLOR textFont:[UIFont systemFontOfSize:ContentFont weight:UIFontWeightLight]];
        [self addSubview:_copLBL];
        [_copLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.line.mas_bottom).with.offset(GAP);
            make.right.equalTo(self.shareLBL.mas_left).with.offset(-GAP);
        }];
    }
    return _copLBL;
}

- (HYLabel *)linkLBL
{
    if (!_linkLBL)
    {
        _linkLBL = [[HYLabel alloc] initWithText:@"https://www.baidu.com" textColor:TEXT_GRAY_COLOR textFont:[UIFont systemFontOfSize:TipsFont weight:UIFontWeightLight]];
        [self addSubview:_linkLBL];
        [_linkLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(GAP);
            make.top.equalTo(self.shoutuLBL.mas_bottom).with.offset(GAP);
        }];
    }
    return _linkLBL;
}

- (HYLabel *)vipLBL
{
    if (!_vipLBL)
    {
        _vipLBL = [[HYLabel alloc] initWithText:@"VIP等级: 3" textColor:TEXT_BLACK_COLOR textFont:[UIFont systemFontOfSize:ContentFont weight:UIFontWeightLight]];
        [self addSubview:_vipLBL];
        [_vipLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(GAP);
            make.top.equalTo(self.linkLBL.mas_bottom).with.offset(GAP);
        }];
    }
    return _vipLBL;
}

- (HYLabel *)myMasterLBL
{
    if (!_myMasterLBL)
    {
        _myMasterLBL = [[HYLabel alloc] initWithText:@"我的师傅: 风清扬ID_20100" textColor:TEXT_BLACK_COLOR textFont:[UIFont systemFontOfSize:ContentFont weight:UIFontWeightLight]];
        [self addSubview:_myMasterLBL];
        [_myMasterLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(GAP);
            make.top.equalTo(self.vipLBL.mas_bottom).with.offset(GAP * 2);
        }];
    }
    return _myMasterLBL;
}

- (HYLabel *)zttgLBL
{
    if (!_zttgLBL)
    {
        _zttgLBL = [[HYLabel alloc] initWithText:@"昨天通过徒弟获得0积分" textColor:TEXT_BLACK_COLOR textFont:[UIFont systemFontOfSize:ContentFont weight:UIFontWeightLight]];
        [self addSubview:_zttgLBL];
        [_zttgLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(GAP);
            make.top.equalTo(self.myMasterLBL.mas_bottom).with.offset(GAP * 2);
        }];
    }
    return _zttgLBL;
}

- (HYButton *)yijianbaishiBtn
{
    if (!_yijianbaishiBtn)
    {
        _yijianbaishiBtn = [[HYButton alloc] initWithTitle:@"一健拜师" backColor: RED_COLOR];
        _yijianbaishiBtn.tag = HYUserCenterViewEventMaster;
        _yijianbaishiBtn.titleLabel.font = [UIFont systemFontOfSize:TipsFont weight:UIFontWeightLight];
        [_yijianbaishiBtn addTarget:self action:@selector(actionForClickedButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_yijianbaishiBtn];
        [_yijianbaishiBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).with.offset(-GAP);
            make.centerY.equalTo(self.myMasterLBL);
        }];
    }
    return _yijianbaishiBtn;
}

- (HYButton *)tudishenqingBtn
{
    if (!_tudishenqingBtn)
    {
        _tudishenqingBtn = [[HYButton alloc] initWithTitle:@"徒弟申请" backColor: THEME_COLOR];
        _tudishenqingBtn.tag = HYUserCenterViewEventPrentice;
        _tudishenqingBtn.titleLabel.font = [UIFont systemFontOfSize:TipsFont weight:UIFontWeightLight];
        [_tudishenqingBtn addTarget:self action:@selector(actionForClickedButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_tudishenqingBtn];
        [_tudishenqingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(self).with.offset(-GAP);
            make.centerY.equalTo(self.zttgLBL);
        }];
    }
    return _tudishenqingBtn;
}

- (HYLabel *)tipsLBL
{
    if (!_tipsLBL)
    {
        _tipsLBL = [[HYLabel alloc] initWithText:@"转发收徒链接且新用户注册后成为我的徒弟\n成功收徒后点击出师解除师徒关系" textColor:TEXT_GRAY_COLOR textFont:[UIFont systemFontOfSize:TipsFont weight:UIFontWeightLight]];
        _tipsLBL.numberOfLines = 0;
        [self addSubview:_tipsLBL];
        [_tipsLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self).with.offset(GAP);
            make.top.equalTo(self.zttgLBL.mas_bottom).with.offset(HYPhotoWidth + GAP * 2);
        }];
    }
    return _tipsLBL;
}

- (void)setModel:(HYUserModel *)model
{
    if (_model != model)
    {
        _model = model;
        self.wallteBtn.content = _model.balance;
    }
}
- (void)setupSubView
{
    NSArray *array = @[@"巅峰商城",@"个人资料",@"消息中心",@"设置中心"];
    NSArray *imgs = @[@"helpcenter_shop_normal",@"helpcenter_user_normal",@"helpcenter_news_normal",@"helpcenter_setting_normal"];
    NSArray *select_imgs = @[@"helpcenter_shop_select",@"helpcenter_user_select",@"helpcenter_new_selecter",@"helpcenter_setting_select"];
    for (int i = 0; i < array.count; i ++)
    {
        int row =  i / 3;//行
        int column = i % 3; //列
        CGFloat new_gap = (SCREEN_WIDTH - GAP * 2 - HYUserCenterButtonWidth * 4) / 4.0;//竖直方向按钮之间的间隙
        CGFloat mas_top = (row < 2)?GAP + row * (HYUserCenterButtonWidth + GAP):GAP + row * (HYUserCenterButtonWidth + GAP) + 30;
  
        HYUserFuncButton *button = [[HYUserFuncButton alloc] initWithFrame:CGRectMake(0, 0, HYUserCenterButtonWidth, HYUserCenterButtonWidth)
                                                                     title:array[i] image:imgs[i] selectImage:select_imgs[i]];
        button.tag = HYUserCenterViewEventStore + i;
        //        button.titleColor = (i == array.count - 2)?THEME_COLOR:((i == array.count - 1)?BLUE_COLOR:button.titleColor);
        button.titleColor = (i == 0)?RED_COLOR:THEME_COLOR;
        
        [button addTarget:self action:@selector(actionForClickedButton:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(button.bounds.size);
            if (IS_APPStore)
                make.bottom.equalTo(self).with.offset(- GAP - TABBAR_HEIGHT);
            else
                make.top.equalTo(self.line.mas_bottom).with.offset(GAP * 2);
            make.left.equalTo(self).with.offset((HYUserCenterButtonWidth + new_gap) * i);
            
        }];
    }
    
}
- (void)actionForClickedButton:(HYButton *)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(actionWithEvent:)])
    {
        [self.delegate actionWithEvent:sender.tag];
    }
}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
